package com.mooc.yohoshop.ui.verifycode;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.blankj.utilcode.util.ToastUtils;
import com.mooc.yohoshop.R;
import com.mooc.yohoshop.dao.UserDaoEntity;
import com.mooc.yohoshop.entity.BaseEntity;
import com.mooc.yohoshop.entity.UserEntity;
import com.mooc.yohoshop.entity.VerifyCodeEntity;
import com.mooc.yohoshop.filed.EventAction;
import com.mooc.yohoshop.ui.base.BaseActivity;
import com.mooc.yohoshop.ui.frame.MainActivity;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.util.HashMap;

public class VerifyCodeActivity extends BaseActivity<VerifyCodeRepo> implements View.OnClickListener {

    public EditText verify_code_pwd,verify_code_mobile;
    private TextView verify_code_tv;
    public int time = 60;
    public boolean flag = false;
    private TimeThread thread;
    @Override
    protected int layout() {
        return R.layout.activity_verify_code;
    }
    @Override
    protected void init() {
        f(R.id.verify_code_back).setOnClickListener(this::onClick);
        verify_code_tv = f(R.id.verify_code_tv);
        verify_code_tv.setOnClickListener(this::onClick);
        f(R.id.verify_code_login).setOnClickListener(this::onClick);
        verify_code_mobile = f(R.id.verify_code_mobile);
        verify_code_pwd = f(R.id.verify_code_pwd);
        thread = new TimeThread(new TimeHand(this));
        thread.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        thread.interrupt();
    }

    @Override
    protected void initData() {

    }
    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof VerifyCodeEntity){
            VerifyCodeEntity verifyCodeEntity = (VerifyCodeEntity) baseEntity;
            verify_code_pwd.setText(verifyCodeEntity.data);
            verify_code_tv.setText("获取验证码");
            flag = false;
        }else if (baseEntity instanceof UserEntity){
            UserEntity userEntity = (UserEntity) baseEntity;
            new UserDaoEntity().insert(userEntity.data);
            startActivity(new Intent(this, MainActivity.class));
            EventAction action = new EventAction();
            action.action = "refreshUser";
            EventBus.getDefault().post(action);
        }
    }
    @Override
    public void onClick(View v) {
        if (R.id.verify_code_back == v.getId())
            finish();
        else if (R.id.verify_code_tv == v.getId()){
            if (TextUtils.isEmpty(verify_code_mobile.getText())){
                ToastUtils.showShort("输入手机号");
                return;
            }
            flag = true;
            HashMap<String,Object>map=new HashMap<>();
            map.put("phone",verify_code_mobile.getText().toString());
            request(repo.requestVerifyCode(map));
        }else {
            HashMap<String,Object>map=new HashMap<>();
            map.put("phone",verify_code_mobile.getText().toString());
            map.put("verifyCode",verify_code_pwd.getText().toString());
            request(repo.requestVerifyCodeLogin(map));
        }
    }

    public static class TimeThread extends Thread{
        private TimeHand hand;
        public TimeThread(TimeHand hand){
            this.hand = hand;
        }
        @Override
        public void run() {
            super.run();
            while (!interrupted()){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                hand.sendEmptyMessage(0);
            }
        }
    }

    public static class TimeHand extends Handler{
        private WeakReference<VerifyCodeActivity>weakReference;
        public TimeHand(VerifyCodeActivity verifyCodeActivity){
            weakReference = new WeakReference<>(verifyCodeActivity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (weakReference.get() == null)
                return;
            weakReference.get().time-=1;
            if (weakReference.get().time<0) {
                weakReference.get().time = 0;
                weakReference.get().flag = false;
                weakReference.get().verify_code_tv.setText("获取验证码");
            }
            if (weakReference.get().flag)
                weakReference.get().verify_code_tv.setText(weakReference.get().time+"S");
        }
    }

}
